<?php
class shop extends core {
	public function __construct() {

		parent::__construct();
	}

	//index
	public function index() {
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($keyword) {
			$where .= "AND (s.name LIKE '%{$keyword}%' OR s.mobile LIKE '%{$keyword}%' OR m.name LIKE '%{$keyword}%' )";
		}
		if ($id) {
			$where .= "AND s.id='{$id}'";
		}
		//分页
		$this->setpages(array('id'=>$id, 'keyword'=>$keyword));

		$sql = "SELECT s.* , m.name as m_name FROM jf_shop s LEFT JOIN jf_member m ON s.member_id = m.id 
				WHERE 1 {$where} ORDER BY s.id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		//二维码url
		if($rs){
			foreach ($rs as $key => $val) {
				$shop_id= $val['id'];
				$url = "{$this->domain}/wap.php?app=shop&act=detail&shop_id={$shop_id}&reseller=";
				$url = urlencode($url);
				$rs[$key]['url'] = $url;
			}
		}
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('where',$where);

		$this->display();
	}

	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$this->display();
		}
	}	

	//add
	public function edit() {
		if (IS_POST) { //添加
			$this->save();
		} else { //显示
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM jf_shop WHERE id='{$id}'";
			$row = $this->db->get_row($sql);
			$this->smarty->assign('row', $row);
			// $this->smarty->assign('citys', $this->_citys('0'));
			$this->display();
		}
	}
	
	//写入到数据库
	public function save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$member_id = (isset($_POST['member_id']) && intval($_POST['member_id'])) ? intval($_POST['member_id']) : 0;
		$mobile = (isset($_POST['mobile']) && trim($_POST['mobile'])) ? trim($_POST['mobile']) : '';
		$tel = (isset($_POST['tel']) && trim($_POST['tel'])) ? trim($_POST['tel']) : '';
		$shop_type = (isset($_POST['shop_type']) && intval($_POST['shop_type'])) ? intval($_POST['shop_type']) : 0;
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$province = (isset($_POST['province']) && trim($_POST['province'])) ? intval($_POST['province']) : 440000;
		$city = (isset($_POST['city']) && trim($_POST['city'])) ? intval($_POST['city']) : 440100;
		$district = (isset($_POST['district']) && trim($_POST['district'])) ? intval($_POST['district']) : 0;
		$address = (isset($_POST['address']) && trim($_POST['address'])) ? trim($_POST['address']) : '';
		$description = (isset($_POST['description']) && trim($_POST['description'])) ? trim($_POST['description']) : '';
		$longitude = (isset($_POST['longitude']) && trim($_POST['longitude'])) ? trim($_POST['longitude']) : '';
		$latitude = (isset($_POST['latitude']) && trim($_POST['latitude'])) ? trim($_POST['latitude']) : '';
		$sort = (isset($_POST['sort']) && intval($_POST['sort'])) ? intval($_POST['sort']) : 999;
		$poster_pic = upload_one_file("shop","poster_pic");

		if ($id > 0) {  //edit
			$sql = "UPDATE jf_shop SET name='{$name}', member_id='{$member_id}', mobile='{$mobile}' , 
					shop_type='{$shop_type}', status='{$status}' , province='{$province}' , city='{$city}', 
					district='{$district}',description='{$description}',address='{$address}',tel='{$tel}',
					longitude='{$longitude}',latitude='{$latitude}',sort='{$sort}' 
					 WHERE id='{$id}'";
			$act = $this->db->query($sql);
			//更新图片
			if($poster_pic){
				$thing = '';
				if($poster_pic){
					$thing .= " poster_pic='{$poster_pic}',";
				}
				$thing=substr($thing,0,strlen($thing)-1);
				$sql = "UPDATE jf_shop SET {$thing} where id = '{$id}'";
				$this->db->query($sql);
			}
		} else { //add
			$sql = "INSERT INTO jf_shop(name, member_id, mobile ,shop_type,province,city,
					district, add_time, status,poster_pic,description,address,tel,longitude,latitude,sort) 
					VALUES('{$name}', '{$member_id}','{$mobile}', '{$shop_type}', '{$province}',
					'{$city}','{$district}', '".time()."', '{$status}','{$poster_pic}',
					'{$description}','{$address}','{$tel}','{$longitude}','{$latitude}','{$sort}')";
			$this->db->query($sql);
		}
		header("Location:?app=shop&act=index");
	}

	//delete
	public function delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM jf_shop WHERE id='{$id}'";
		if($this->db->query($sql)){	//删除该商店被收藏
			$sql="DELETE FROM jf_favorite WHERE item_id='{$id}' AND type_id = 2";
			$this->db->query($sql);
		}
		header("Location:?app=shop&act=index");
	}
	public function qrcode_out(){
		$name="";
		$where = (isset($_GET['where']) && trim($_GET['where'])) ? trim($_GET['where']) : '';
		$where = stripslashes($where);
		$sql = "SELECT id,name  FROM jf_shop s WHERE 1 {$where} ORDER BY id DESC";
		$row = $this->db->get_results($sql);
		//创建目录
		$path = ROOT_PATH.UPLOAD_PATH.'/qrcode/'.time().rand(000,999);
		mkdir($path,$mode = 0777);
		foreach ($row as $val) {
			$shop_id = $val->id;
			$url = "{$this->domain}/wap.php?app=shop&act=detail&shop_id={$shop_id}&reseller=";
			$name= $val->name;
			$name =$this->replace_specialChar($name);
			$name = iconv('UTF-8', 'GB2312//IGNORE', $name);
			$this->qr_out($url,$name,$path);
		};
		require_once (FRAMEWORK_PATH .'/third/phpqrcode/PHPZip.php');
		// $path = ROOT_PATH.UPLOAD_PATH.'/qrcode';
		$zip = new PHPZip();
		//压缩并下载并
		$zip->ZipAndDownload($path);
		//删除文件夹
		rmdir($path);

	}
	//导出二维码
	public function qr_out($url,$name,$path){
		require_once (FRAMEWORK_PATH .'/third/phpqrcode/phpqrcode.php');
		return QRcode::pngg($url,$name,$path,$outfile = true,$saveandprint=true);
	}
	//生成二维码
	public function qrcode(){
		require_once (FRAMEWORK_PATH .'/third/phpqrcode/phpqrcode.php');
		$url = urldecode($_GET["url"]);
		return QRcode::png($url);
	}
	//删除字符串中的特殊字符
	public function replace_specialChar($strParam){
    	$regex = "/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\-|\=|\\\|\|\、|\、|\s|/";
    	return preg_replace($regex,"",$strParam);
	}
}
